<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">id</label>
        <el-input v-model="query.id" clearable placeholder="id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">供应商名称</label>
        <el-input v-model="query.name" clearable placeholder="供应商名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">联系方式</label>
        <el-input v-model="query.contactType" clearable placeholder="联系方式" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否配送</label>
        <el-input v-model="query.delivery" clearable placeholder="是否配送" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="600px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="供应商名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="供货范围">
            <el-input v-model="form.supplyScope" style="width: 370px;" placeholder="单位米" />
          </el-form-item>
          <el-form-item label="简介">
            <el-input v-model="form.synopsis" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input v-model="form.contactType" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否配送">
            <el-radio v-model="form.delivery" :label="true">是</el-radio>
            <el-radio v-model="form.delivery" :label="false">否</el-radio>
          </el-form-item>
          <el-form-item label="经度">
            <el-input v-model="form.longitude" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="纬度">
            <el-input v-model="form.latitude" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="name" label="供应商名称" />
        <el-table-column prop="supplyScope" label="供货范围" />
        <el-table-column prop="synopsis" label="简介" />
        <el-table-column prop="contactType" label="联系方式" />
        <el-table-column prop="delivery" label="是否配送">
          <template slot-scope="scope">
            {{ dict.label.is_delivery[scope.row.delivery] }}
          </template>
        </el-table-column>
        <el-table-column prop="longitude" label="经度" />
        <el-table-column prop="latitude" label="纬度" />
        <el-table-column v-permission="['admin','ycSupplier:edit','ycSupplier:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudYcSupplier from '@/api/goods/supplier'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, supplyScope: null, synopsis: null, contactType: null, delivery: null, longitude: null, latitude: null }
export default {
  name: 'YcSupplier',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['is_delivery'],
  cruds() {
    return CRUD({ title: '供货商', url: 'api/ycSupplier', sort: 'id,desc', crudMethod: { ...crudYcSupplier }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'ycSupplier:add'],
        edit: ['admin', 'ycSupplier:edit'],
        del: ['admin', 'ycSupplier:del']
      },
      rules: {
        id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '供应商名称不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'name', display_name: '供应商名称' },
        { key: 'supplyScope', display_name: '供货范围' },
        { key: 'synopsis', display_name: '简介' },
        { key: 'contactType', display_name: '联系方式' },
        { key: 'delivery', display_name: '是否配送' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 编辑 - 之前
    [CRUD.HOOK.beforeToEdit]() {
      console.log('form============')
      console.log(this.form)
      return true
    },
    // 新建/编辑" 验证 - 之后
    [CRUD.HOOK.afterValidateCU]() {
      const _t = this
      console.log(_t.form)
      if (_t.form.delivery == true) {
        _t.form.delivery = 1
      } else {
        _t.form.delivery = 0
      }

      return true
    }
  }
}
</script>

<style scoped>

</style>
